﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_ProductType_StepManage : System.Web.UI.Page
{
    private ProductType_Step ps;
    private string productTypeID { get { return ViewState["ProductTypeID"].ToString(); } set { ViewState["ProductTypeID"] = value; } }

    public ProductManage_RFProductManage_Manage_ProductType_StepManage()
    {
        ps = new ProductType_Step();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ProductTypeID"] != null)
            {
                productTypeID = Request.QueryString["ProductTypeID"].ToString();
                BindGV();
            }
        }
    }

    protected void BindGV()
    {
        this.GridView1.DataSource = ps.GetStepbyPID(productTypeID);
        this.GridView1.DataBind();
    }
    
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGV();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if ((((DropDownList)e.Row.FindControl("DDLPostStep")) != null) && (((DropDownList)e.Row.FindControl("DDLNextStep")) != null))
        {
            DropDownList ddlps = (DropDownList)e.Row.FindControl("DDLPostStep");
            BindDDL(ddlps);
            ddlps.SelectedValue = ((HiddenField)e.Row.FindControl("HDFPostStep")).Value;

            DropDownList ddlns = (DropDownList)e.Row.FindControl("DDLNextStep");
            BindDDL(ddlns);
            ddlns.SelectedValue = ((HiddenField)e.Row.FindControl("HDFNextStep")).Value;
        }
        //
        if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
        {
            DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
            if (((Label)d.Rows[0].Cells[1].FindControl("LBLProductTypeID")) != null)
            {
                Label lblProductTypeID = (Label)d.Rows[0].Cells[1].FindControl("LBLProductTypeID");
                lblProductTypeID.Text = productTypeID;
            }
            if (((DropDownList)d.Rows[1].Cells[1].FindControl("DDLStepItem")) != null)
            {
                DropDownList ddlsi = (DropDownList)d.Rows[1].Cells[1].FindControl("DDLStepItem");
                BindDDL(ddlsi);
            }
            if (((DropDownList)d.Rows[3].Cells[1].FindControl("DDLPostStep")) != null)
            {
                DropDownList ddlps1 = (DropDownList)d.Rows[3].Cells[1].FindControl("DDLPostStep");
                BindDDL(ddlps1);
            }
            if (((DropDownList)d.Rows[4].Cells[1].FindControl("DDLNextStep")) != null)
            {
                DropDownList ddlns1 = (DropDownList)d.Rows[4].Cells[1].FindControl("DDLNextStep");
                BindDDL(ddlns1);
            }
        }
    }

    protected void BindDDL(DropDownList ddl)
    {
        ddl.DataSource = RFSourceStep.list;
        ddl.DataTextField = "StepText";
        ddl.DataValueField = "StepValue";
        ddl.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        ps.Delete(id);
        BindGV();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindGV();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ProductType_StepInfo productTypeInfo = new ProductType_StepInfo();//(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString(), GridView1.DataKeys[e.RowIndex].Values[2].ToString(), GridView1.DataKeys[e.RowIndex].Values[3].ToString());
        productTypeInfo.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        productTypeInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        productTypeInfo.StepItem = GridView1.DataKeys[e.RowIndex].Values[2].ToString();
        productTypeInfo.StepIndex = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim());
        productTypeInfo.PostStep = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPostStep"))).SelectedValue;
        productTypeInfo.NextStep = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLNextStep"))).SelectedValue;

        ps.Update(productTypeInfo);
        GridView1.EditIndex = -1;
        BindGV();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ProductType_StepInfo productTypeInfo = new ProductType_StepInfo();
        productTypeInfo.ID = 0;
        productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        if (((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLStepItem"))).SelectedValue == null)
        {
            string str1 = "请选择一个工序名称！";
            ClientScript.RegisterStartupScript(this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        productTypeInfo.StepItem = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLStepItem"))).SelectedValue;
        int stepIndex;
        if (!int.TryParse(((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim(), out stepIndex))
        {
            string str1 = "工序序号中，请输入有效的正整数！";
            ClientScript.RegisterStartupScript(this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        productTypeInfo.StepIndex = stepIndex;
        productTypeInfo.PostStep = ((DropDownList)(d.Rows[3].Cells[1].FindControl("DDLPostStep"))).SelectedValue;
        productTypeInfo.NextStep = ((DropDownList)(d.Rows[4].Cells[1].FindControl("DDLNextStep"))).SelectedValue; 

        ProductType_StepInfo pro = ps.GetStepbyPIDItem(productTypeInfo.ProductTypeID,productTypeInfo.StepItem);
        if (!string.IsNullOrEmpty(pro.ProductTypeID))
        {
            string str1 = "加不了啦，此产品类型下的此工序列已存在！";
            ClientScript.RegisterStartupScript(this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            ps.Insert(productTypeInfo);
            string str1 = "添加成功！";
            ClientScript.RegisterStartupScript(this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindGV();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindGV();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
